package hlhj.fhp.checkpoint.atys

import android.graphics.Color
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import hlhj.fhp.checkpoint.BaseAty
import hlhj.fhp.checkpoint.Constant
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.adp.ShareAdp
import hlhj.fhp.checkpoint.customView.ChooseLevelDialog
import hlhj.fhp.checkpoint.iview.ShareView
import hlhj.fhp.checkpoint.javaBean.ShareBean
import hlhj.fhp.checkpoint.presenter.SharePresenter
import kotlinx.android.synthetic.main.aty_share.*
import kotlinx.android.synthetic.main.lo_pub_head.*
import kotlinx.android.synthetic.main.lo_publist.*
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject
import android.graphics.BitmapFactory
import android.graphics.Bitmap
import android.R.attr.description
import android.os.Build
import com.example.mymvp.log
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import hlhj.fhp.checkpoint.utils.ImageDispose
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import hlhj.fhp.checkpoint.http.Urls
import hlhj.fhp.checkpoint.utils.ToastUtils


/**
 * Created by Never Fear   on 2018\11\13 0013.
Never More....
 */
class ShareAty :BaseAty<ShareView,SharePresenter>(), ShareView, SpringView.OnFreshListener, ChooseLevelDialog.ClickResultListener {
    private var shareAdp:ShareAdp?=null
    private var page=1;
    private var type=-1
    private var level=1 //1-A级 2-B级
    private val datas=ArrayList<ShareBean.InfoBean.ListBean.DataBean>()
    private var chooseLevelDialog:ChooseLevelDialog?=null
    private var api:IWXAPI?=null
    override fun initView() {
        shareAdp= ShareAdp(datas)
        listView.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        listView.adapter=shareAdp
        btRightIcon.setImageResource(R.drawable.bt_share)
        btRightIcon.visibility=View.VISIBLE

        chooseLevelDialog= ChooseLevelDialog(this,this)
        spView.footer=DefaultFooter(this)
        spView.header=DefaultHeader(this)
        WXAPIFactory.createWXAPI(this,Constant.WXAPPID)
        api=WXAPIFactory.createWXAPI(this,Constant.WXAPPID)
    }
    override fun logicStart() {
        presenter?.getSharelist(-1,page,level,this)
        shareAdp?.notifyDataSetChanged()
    }

    override fun click(t: String) {
        if (t.equals("A级")){
            btAll.text="A级"
            level=1
        }else{
            btAll.text="B级"
            level=2
        }
        btAll.setTextColor(Color.parseColor("#FF1290F2"))
         btYes.setTextColor(Color.parseColor("#FF585F65"))
        btNo.setTextColor(Color.parseColor("#FF585F65"))
        page=1
        type=-1
        presenter?.getSharelist(type,page,level,this)
    }

    override fun setListener() {
        spView.setListener(this)

        btAll.setOnClickListener {
            chooseLevelDialog?.show()

        }
        btYes.setOnClickListener {
            btYes.setTextColor(Color.parseColor("#FF1290F2"))
            btAll.setTextColor(Color.parseColor("#FF585F65"))
            btNo.setTextColor(Color.parseColor("#FF585F65"))
            page=1
            type=1
            presenter?.getSharelist(type,page,level,this)

        }
        btNo.setOnClickListener {
            type=0
            btNo.setTextColor(Color.parseColor("#FF1290F2"))
            btYes.setTextColor(Color.parseColor("#FF585F65"))
            btAll.setTextColor(Color.parseColor("#FF585F65"))
            page=1
            presenter?.getSharelist(type,page,level,this)

        }
        btRightIcon.setOnClickListener {
            val webpage = WXWebpageObject()
            webpage.webpageUrl = Urls.shareUrl

            val msg = WXMediaMessage(webpage)
            msg.title = "一车通 "
            msg.description = "快来加入一车通吧"
            val thumbBmp = BitmapFactory.decodeResource(resources, R.mipmap.logo)

            log("fhp","手机型号${Build.MODEL.toString()}")
            if (Build.MODEL.toString().equals("SM-G9550")){

            }else{
                msg.thumbData = ImageDispose.Bitmap2Bytes(thumbBmp)

            }

            val req = SendMessageToWX.Req()
            req.transaction = "webpage"
            req.message = msg

            api?.sendReq(req)
        }
    }

    override fun setShare(shareBean: ShareBean) {
        spView.onFinishFreshAndLoad()
        if (page==1){
            datas.clear()
        }
        datas.addAll(shareBean.info.list.data)
        shareAdp?.notifyDataSetChanged()

        btYes.text="已激活(${shareBean?.info.active}人)"
        btNo.text="未激活(${shareBean?.info.no_active}人)"

    }

    override fun onLoadmore() {
        page++
        presenter?.getSharelist(type,page,level,this)

    }

    override fun onRefresh() {
        page=1
        presenter?.getSharelist(type,page,level,this)
    }

    override fun bindView(): ShareView {
        return this
    }

    override fun createPresenter(): SharePresenter {
        return SharePresenter()
    }

    override val contentId: Int= R.layout.aty_share
}